#include <stdlib.h>
#include "list.h"
#include "error.h"
#include "gp_list.h"

void gp_node_add_tail(void *node_addr, struct list_head *head)
{
	if(!head)
		return;

	if(!node_addr) 
		return;
	
	gp_node_t *new = malloc(sizeof(gp_node_t));
	
	if(!new){
		SYSTEM_FATAL_ERR(MEM_ALLOC_FAILED);
	}

	new->addr = node_addr;
	list_add_tail(&new->entry, head);
}


// 注意，头节点不释放
void gp_list_free(struct list_head *head)
{
	if(!head)
		return;
		
	gp_node_t *cur, *next;

	list_for_each_entry_safe(cur, next, head, entry){
		list_del(&cur->entry);
		free(cur);
	}
}


// addr:数据的地址 (gp_node_t 的 void *addr 成员)
void gp_list_del_node(void *addr, struct list_head *head)
{
	gp_node_t *cur;
	list_for_each_entry(cur, head, entry){
		if(cur->addr == addr) {
			list_del(&cur->entry);
			free(cur);
			break;
		}
	}
}